export function isNull(value) {
  return value === null || value === undefined;
}

export const generateId = function () {
  return ((Date.now() / 1000) | 0).toString(36) + Math.random().toString(36).slice(-4);
};

export const deepClone = function (
  data,
  apply = function (data, label) {
    return data[label];
  }
) {
  if (!data || typeof data !== "object") {
    return data;
  }
  if (Array.isArray(data)) {
    return data.map(_ => deepClone(_, apply));
  }
  const newData = {};
  const dataKeys = Object.keys(data);
  dataKeys.forEach(label => {
    const value = apply(data, label);
    newData[label] = deepClone(value, apply);
  });
  return newData;
};

export const flatWidget = (widgetList, subFormFlag = false) => {
  if (widgetList) {
    return widgetList.flatMap(widget => {
      if (subFormFlag || widget.type !== "SubForm") {
        return [widget, ...flatWidget(widget.widgetList)];
      } else {
        return [];
      }
    });
  }
  return [];
};

export const flatSubFormWidget = widgetList => {
  if (widgetList) {
    return widgetList.flatMap(widget => {
      if (widget.type === "SubForm") {
        return [widget];
      } else {
        return flatSubFormWidget(widget.widgetList);
      }
    });
  }
  return [];
};
